package com.hello.user.service.impl;

import com.hello.page.PageResponse;
import com.hello.user.convert.TestConvert;
import com.hello.user.entity.po.Test;
import com.hello.user.mapper.TestMapper;
import com.hello.user.entity.req.TestReq;
import com.hello.user.service.TestService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Test)表服务实现类
 *
 * @author makejava
 * @since 2025-02-11 11:44:31
 */
@Service("testService")
public class TestServiceImpl implements TestService {
    @Resource
    private TestMapper testMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Test queryById(Long id) {
        return this.testMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param testReq 筛选条件
     * @return 查询结果
     */
    @Override
    public PageResponse<Test> queryByPage(TestReq testReq) {
        Test test = TestConvert.INSTANCE.convertReqToTest(testReq);
        PageResponse<Test> pageResponse = new PageResponse<>();
        pageResponse.setCurrent(testReq.getPageNo());
        pageResponse.setPageSize(testReq.getPageSize());
        Long pageStart = (testReq.getPageNo() - 1) * testReq.getPageSize();
        long total = this.testMapper.count(test);
        List<Test> testList = this.testMapper.queryAllByLimit(test, pageStart, testReq.getPageSize());
        pageResponse.setTotal(total);
        pageResponse.setRecords(testList);
        return pageResponse;
    }

    /**
     * 新增数据
     *
     * @param test 实例对象
     * @return 实例对象
     */
    @Override
    public Test insert(Test test) {
        this.testMapper.insert(test);
        return test;
    }

    /**
     * 修改数据
     *
     * @param test 实例对象
     * @return 实例对象
     */
    @Override
    public Test update(Test test) {
        this.testMapper.update(test);
        return this.queryById(test.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.testMapper.deleteById(id) > 0;
    }
}
